<template>
  <view class="avatar">
    <view @click="onAvatarChange" class="avatar-content">
      <image class="image" :src="avatarUrl || '/static/my/avatar.jpg'" />
      <view class="edit-icon">
        <text class="icon">✏️</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      avatarUrl: ''
    };
  },
  methods: {
    onAvatarChange() {
      uni.chooseMedia({
        count: 1,
        mediaType: ['image'],
        success: (res) => {
          const tempFilePath = res.tempFiles[0].tempFilePath;
          uni.uploadFile({
            url: 'https://your-server-upload-url',
            filePath: tempFilePath,
            name: 'file',
            success: (uploadFileRes) => {
              const data = JSON.parse(uploadFileRes.data);
              this.avatarUrl = data.url;
            },
            fail: (error) => {
              console.error('上传失败:', error);
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="scss">
@import '../styles.scss';
</style>
